package com.hwy.simple.chart

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hwy.simple.chart.line_chart.LineChartActivity

class MainActivity : AppCompatActivity() {

    private val dataList = arrayOf(
        arrayOf("折线图", LineChartActivity::class.java),
    )

    private lateinit var recyclerView: RecyclerView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initRecyclerView()

    }

    private fun initRecyclerView() {
        recyclerView = findViewById<RecyclerView>(R.id.recycler_view).apply {
            layoutManager = LinearLayoutManager(context)
            adapter = Adapter()
        }

    }

    inner class Adapter : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
            return ViewHolder(
                LayoutInflater.from(parent.context).inflate(R.layout.adapter_main, parent, false)
            )
        }

        override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
            (holder as? ViewHolder)?.bindView(position)
        }

        override fun getItemCount(): Int {
            return dataList.size
        }

    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        fun bindView(position: Int) {
            itemView.findViewById<Button>(R.id.btn)?.run {
                text = "${dataList[position][0]}"
                setOnClickListener {
                    startActivity(Intent(this@MainActivity, dataList[position][1] as Class<*>))
                }
            }
        }

    }

}